// pages/questionSurvey/questionSurvey.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    answers: [],
    questionnaire: [],
    isSubmit:false,
    placeVal:[],
    focuslist:[],
    valLen:null,
    minLen:null,
    tscore:'',
    deadTime:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.request({
      url: app.globalData.baseUrl + '/quesSurvey/findByStudentIdByformalClassId' + '/' + options.stuId + '/' + options.formalClassId,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        if (res.data.code == 0) {
          let myQuestionnareList = res.data.data.sendQuesSurvey.questionnaire
          that.setData({
            questionnaire: res.data.data.sendQuesSurvey.questionnaire,
            deadTime: res.data.data.sendQuesSurvey.deadline
          })
          that.checkDeno()


          let placeholderList = []
          // let focuslist=[];
          that.data.questionnaire.topicList.forEach((item, key, arr) => {
            if (item.topicType == '3') {
              placeholderList[item.id]='无'
              // focuslist[item.id]=false
            }
          })
          that.setData({
            'placeVal': placeholderList
            // 'focuslist': focuslist
          })
        }
      }
    })

    // 导航栏标题
    wx.setNavigationBarTitle({
      title: '已结课'
    })
  },
  onShow() {
    app.globalData.stopMoreClick = false
    },
  // 答案输入
  setAnswer(e){
    // console.log(e)
    this.data.questionnaire.topicList.forEach((item, key, arr) => {
      if (item.id == e.currentTarget.dataset.id) {
        item.answer = e.detail.value
        let opList = item.topicOptions
        opList.forEach(it=>{
          if (it.topicContent == e.detail.value){
            let tsc=it.score
            this.setData({
              tscore: tsc
            })
          }
        })
        item.score=this.data.tscore
        this.setData({
          questionnaire: this.data.questionnaire
        })
        // console.log(this.data.questionnaire)
      }
    })
    this.checkDeno()
  },
  onFacus(e) {
    let dataid = e.currentTarget.dataset.id;
    // let focuslist = this.data.focuslist;
    // focuslist[dataid] = true;
    let placeholderlist = this.data.placeVal;
    placeholderlist[dataid]='';
    this.setData({
      placeVal: placeholderlist
      // 'focuslist': focuslist
    })
  },
  onBlur(e) {
    // console.log(e.currentTarget.dataset.topic)
    if (e.currentTarget.dataset.topic == '3') {
      let did = e.currentTarget.dataset.id;
      // let focuslist = this.data.focuslist;
      // focuslist[did] = false;
      // console.log(focuslist)
      let placeholderlist = this.data.placeVal;
      placeholderlist[did] = '无';
      var answer = this.data.answers;
      var dataid = e.currentTarget.dataset.id;
      var obj = {
        dataid: dataid,
        valLen: e.detail.value.length,
        minLen: e.currentTarget.dataset.minlen
      }
      var flag = true;
      for (var i = 0; i < answer.length; i++) {
        if (answer[i].dataid == dataid) {
          answer[i] = obj;
          flag = false;
          break;
        }
      }
      if (flag) {
        answer.push(obj);
      }
      // console.log(answer)
      this.setData({
        answers: answer, 
        placeVal: placeholderlist
        // focuslist: focuslist
      })
    }
  },
  // 提交问卷内容
  submitQuetionnaire(){
    if(this.data.isSubmit)
    {
      // console.log(this.data.answers)
      let answer = this.data.answers;
      for (var i = 0; i < answer.length; i++) {
        if (answer[i].minLen > answer[i].valLen) {
          // let focuslist = this.data.focuslist;
          // focuslist[answer[i].dataid]=true;
          // this.setData({
          //   'focuslist': focuslist
          // })
          wx.showToast({
            title: '字数不够哟~'
          })
          return;
        }
      }
      if (app.globalData.stopMoreClick) return
      app.globalData.stopMoreClick = true
      wx.request({
        url: app.globalData.baseUrl + '/quesSurvey/submitTeacher',
        data: {
          questionnaire: this.data.questionnaire
        },
        method: 'POST',
        header: {
          "Content-Type": "application/json"
        },
        success(res) {
          if (res.data.code == 0) {
            // console.log(wx.getStorageSync('sevenData'))
            wx.redirectTo({
              url: `/pages/detail/detailGrade/detailGrade?status=${wx.getStorageSync('sevenData').status}&coursename=${wx.getStorageSync('sevenData').coursename}`
            })
          }
        }
      })
    }
  },
  checkDeno() {
    let checkPass = true
    this.data.questionnaire.topicList.forEach((item, key, arr) => {
      // if (item.answer && item.answer.length == 0) {
      //   checkPass = false
      // }
      if (item.isRequired == '1' && !item.answer) {
        checkPass = false
      }
    })
    this.setData({
        isSubmit: checkPass
    })
    // console.log(this.data.doSubmit)
  }
})